<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
			<el-form-item label="医生姓名" prop="doctorName">
				<el-input
					v-model="queryParams.doctorName"
					placeholder="请输入医生姓名"
					clearable
					size="small"
					@keyup.enter.native="handleQuery"
				/>
			</el-form-item>
			<el-form-item label="手机号码" prop="phone">
				<el-input
					v-model="queryParams.phone"
					placeholder="请输入医生姓名"
					clearable
					size="small"
					@keyup.enter.native="handleQuery"
				/>
			</el-form-item>
			<el-form-item label="提现类型" prop="type">
				<el-select v-model="queryParams.type" placeholder="请选择提现类型" clearable size="small">
					<el-option label="请选择字典生成" value=""/>
				</el-select>
			</el-form-item>
			<el-form-item label="提现状态" prop="status">
				<el-select v-model="queryParams.status" placeholder="请选择提现状态" clearable size="small">
					<el-option label="待审核" :value="0"/>
					<el-option label="已打款" :value="1"/>
					<el-option label="已驳回" :value="2"/>
				</el-select>
			</el-form-item>
<!--			<el-form-item label="审核时间" prop="auditTime">
				<el-date-picker clearable size="small" style="width: 200px"
								v-model="queryParams.auditTime"
								type="date"
								value-format="yyyy-MM-dd"
								placeholder="选择审核时间">
				</el-date-picker>
			</el-form-item>-->
			<el-form-item>
				<el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
<!--			<el-col :span="1.5">
				<el-button
					type="primary"
					icon="el-icon-plus"
					size="mini"
					@click="handleAdd"
				>新增
				</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button
					type="success"
					icon="el-icon-edit"
					size="mini"
					:disabled="single"
					@click="handleUpdate"
				>修改
				</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button
					type="danger"
					icon="el-icon-delete"
					size="mini"
					:disabled="multiple"
					@click="handleDelete"
				>删除
				</el-button>
			</el-col>-->
			<el-col :span="1.5">
				<el-button
					type="warning"
					icon="el-icon-download"
					size="mini"
					@click="handleExport"
				>导出
				</el-button>
			</el-col>
			<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table v-loading="loading" :data="withdrawalList" @selection-change="handleSelectionChange">
			<el-table-column label="序号" type="index" :index="indexMethod" width="55" align="center"/>
			<el-table-column label="医生姓名" align="center" prop="doctorName"/>
			<el-table-column label="手机号码" align="center" prop="phone"/>
			<el-table-column label="提现类型" align="center" prop="type"/>
			<el-table-column label="提现积分" align="center" prop="point"/>
			<el-table-column label="提现账户" align="center" prop="account"/>
			<el-table-column label="银行名称" align="center" prop="bankName"/>
			<el-table-column label="提现状态" align="center" prop="status">
				<template slot-scope="scope">
					<span v-if="scope.row.status == 0"><el-tag type="warning">待审核</el-tag></span>
					<span v-if="scope.row.status == 1"><el-tag type="success">已打款</el-tag></span>
					<span v-if="scope.row.status == 2"><el-tag type="danger">已驳回</el-tag></span>
				</template>
			</el-table-column>
			<el-table-column label="创建时间" align="center" prop="createTime" width="180">
				<template slot-scope="scope">
					{{scope.row.createTime}}
				</template>
			</el-table-column>
			<el-table-column label="审核时间" align="center" prop="auditTime" width="180">
				<template slot-scope="scope">
					<span>{{ parseTime(scope.row.auditTime, '{y}-{m}-{d}') }}</span>
				</template>
			</el-table-column>
			<el-table-column label="操作" align="center">
				<template slot-scope="scope">
					<el-button
						size="mini"
						type="text"
						icon="el-icon-edit"
						:disabled="scope.row.status != 0"
						@click="handleAudit(scope.row)"
					>审核
					</el-button>
				</template>
			</el-table-column>
		</el-table>

		<pagination
			v-show="total>0"
			:total="total"
			:page.sync="queryParams.pageNum"
			:limit.sync="queryParams.pageSize"
			@pagination="getList"
		/>

		<!-- 添加或修改提现对话框 -->
		<el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
			<el-form ref="form" :model="form" :rules="rules" label-width="80px">
				<el-form-item label="医生id" prop="doctorId">
					<el-input v-model="form.doctorId" placeholder="请输入医生id"/>
				</el-form-item>
				<el-form-item label="医生姓名" prop="doctorName">
					<el-input v-model="form.doctorName" placeholder="请输入医生姓名"/>
				</el-form-item>
				<el-form-item label="提现类型" prop="type">
					<el-select v-model="form.type" placeholder="请选择提现类型">
						<el-option label="请选择字典生成" value=""/>
					</el-select>
				</el-form-item>
				<el-form-item label="提现积分" prop="point">
					<el-input v-model="form.point" placeholder="请输入提现积分"/>
				</el-form-item>
				<el-form-item label="提现账户" prop="account">
					<el-input v-model="form.account" placeholder="请输入提现账户"/>
				</el-form-item>
				<el-form-item label="提现账户" prop="bankName">
					<el-input v-model="form.bankName" placeholder="请选择银行名称"/>
				</el-form-item>
				<el-form-item label="提现状态 0待财务审核 1已打款 0已驳回">
					<el-radio-group v-model="form.status">
						<el-radio label="1">请选择字典生成</el-radio>
					</el-radio-group>
				</el-form-item>
				<el-form-item label="审核时间" prop="auditTime">
					<el-date-picker clearable size="small" style="width: 200px"
									v-model="form.auditTime"
									type="date"
									value-format="yyyy-MM-dd"
									placeholder="选择审核时间">
					</el-date-picker>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm">确 定</el-button>
				<el-button @click="cancel">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
import {
	listWithdrawal,
	getWithdrawal,
	delWithdrawal,
	addWithdrawal,
	updateWithdrawal,
	exportWithdrawal
} from "@/api/sunshine/withdrawal";
import { returnPoint } from '@/api/point/point'

export default {
	name: "Withdrawal",
	data() {
		return {
			// 遮罩层
			loading: true,
			// 选中数组
			ids: [],
			// 非单个禁用
			single: true,
			// 非多个禁用
			multiple: true,
			// 显示搜索条件
			showSearch: true,
			// 总条数
			total: 0,
			// 提现表格数据
			withdrawalList: [],
			// 弹出层标题
			title: "",
			// 是否显示弹出层
			open: false,
			// 查询参数
			queryParams: {
				pageNum: 1,
				pageSize: 10,
				doctorName: null,
				phone: null,
				type: null,
				point: null,
				account: null,
				bankName: null,
				status: null,
				auditTime: null,
				orderByColumn: 'create_time',
				isAsc: 'desc'
			},
			// 表单参数
			form: {},
			// 表单校验
			rules: {}
		};
	},
	created() {
		this.getList();
	},
	methods: {
		/** 查询提现列表 */
		getList() {
			this.loading = true;
			listWithdrawal(this.queryParams).then(response => {
				this.withdrawalList = response.rows;
				this.total = response.total;
				this.loading = false;
			});
		},
		// 取消按钮
		cancel() {
			this.open = false;
			this.reset();
		},
		// 表单重置
		reset() {
			this.form = {
				withdrawalId: null,
				doctorId: null,
				doctorName: null,
				type: null,
				point: null,
				account: null,
				bankName: null,
				status: 0,
				auditTime: null,
				createBy: null,
				createTime: null,
				updateBy: null,
				updateTime: null
			};
			this.resetForm("form");
		},
		/** 搜索按钮操作 */
		handleQuery() {
			this.queryParams.pageNum = 1;
			this.getList();
		},
		/** 重置按钮操作 */
		resetQuery() {
			this.resetForm("queryForm");
			this.handleQuery();
		},
		// 多选框选中数据
		handleSelectionChange(selection) {
			this.ids = selection.map(item => item.withdrawalId)
			this.single = selection.length !== 1
			this.multiple = !selection.length
		},
		/** 新增按钮操作 */
		handleAdd() {
			this.reset();
			this.open = true;
			this.title = "添加提现";
		},
		/** 修改按钮操作 */
		handleUpdate(row) {
			this.reset();
			const withdrawalId = row.withdrawalId || this.ids
			getWithdrawal(withdrawalId).then(response => {
				this.form = response.data;
				this.open = true;
				this.title = "修改提现";
			});
		},
		/** 提交按钮 */
		submitForm() {
			this.$refs["form"].validate(valid => {
				if (valid) {
					if (this.form.withdrawalId != null) {
						updateWithdrawal(this.form).then(response => {
							if (response.code === 200) {
								this.msgSuccess("修改成功");
								this.open = false;
								this.getList();
							}
						});
					} else {
						addWithdrawal(this.form).then(response => {
							if (response.code === 200) {
								this.msgSuccess("新增成功");
								this.open = false;
								this.getList();
							}
						});
					}
				}
			});
		},
		updateWithdrawal(withdrawal) {
			updateWithdrawal(withdrawal).then(response => {
				if (response.code === 200) {
					if(withdrawal.status == 2) {
						let params = {
							pointEventValue: withdrawal.point,
							doctorId: withdrawal.doctorId
						}
						returnPoint(params);
					}
					this.msgSuccess("修改成功");
					this.open = false;
					this.getList();
				}
			});
		},
		/** 删除按钮操作 */
		handleDelete(row) {
			const withdrawalIds = row.withdrawalId || this.ids;
			this.$confirm('是否确认删除提现编号为"' + withdrawalIds + '"的数据项?', "警告", {
				confirmButtonText: "确定",
				cancelButtonText: "取消",
				type: "warning"
			}).then(function () {
				return delWithdrawal(withdrawalIds);
			}).then(() => {
				this.getList();
				this.msgSuccess("删除成功");
			}).catch(function () {
			});
		},
		/** 导出按钮操作 */
		handleExport() {
			const queryParams = this.queryParams;
			this.$confirm('是否确认导出所有提现数据项?', "警告", {
				confirmButtonText: "确定",
				cancelButtonText: "取消",
				type: "warning"
			}).then(function () {
				return exportWithdrawal(queryParams);
			}).then(response => {
				this.download(response.msg);
			}).catch(function () {
			});
		},
		indexMethod(index) {
			return (this.queryParams.pageNum - 1) * this.queryParams.pageSize + index + 1;
		},
		handleAudit(row) {
			this.$confirm('请您确认审核状态，该操作会同步至医生端...', '提示', {
				distinguishCancelAndClose: true,
				confirmButtonText: '通过',
				cancelButtonText: '驳回',
				type: 'warning'
			}).then(() => {
				row.status = 1;
				this.updateWithdrawal(row);
			}).catch(action => {
				console.log(action)
				if(action === 'cancel') {
					row.status = 2;
					this.updateWithdrawal(row);
				}
			});
		}
	}
};
</script>
